package controller;

import model.PersistenceType;
import model.ReturnDTO;
import model.Teacher;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.TeacherService;

/** 
 *
 * @author Moraes
 */
public class TeacherController extends SimpleFormController {

    private TeacherService teacherService;

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public TeacherController() {
        setCommandClass(Teacher.class);
        setCommandName("teacherCommand");
        setSuccessView("successView");
        setFormView("teacherView");
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        Teacher teacher = (Teacher) command;
        ReturnDTO returnDTO = new ReturnDTO();

        returnDTO = teacherService.PersistTeacher(teacher, PersistenceType.INSERT);
        if (returnDTO.result) {
            ModelAndView mv = new ModelAndView(getSuccessView());
            mv.addObject("successMessage", "Cadastro realizado com sucesso");
            return mv;
        } else {
            ModelAndView mv = new ModelAndView(getFormView());
            mv.addObject("errorMessage", "Ocorreu um Erro." + returnDTO.errorMessage);
            return mv;
        }
    }

    
//    @Override
//    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        ModelAndView mv = new ModelAndView(getFormView());
//        List<Teacher> teacherList = this.teacherService.getAll();
//        mv.addObject("teacherList", teacherList);
//        return mv;
//    }
}
